package com.gs.SynPatrolRep.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.adapter.base.BaseAdapter;
import com.gs.SynPatrolRep.adapter.base.BaseViewHolder;
import com.gs.SynPatrolRep.db.dto.PalBillItemDto;

import java.util.List;

public class PalBillItemAdapter extends BaseAdapter<PalBillItemDto> {

    public PalBillItemAdapter(Context context, List<PalBillItemDto> mData, int layoutId) {
        super(context, mData, layoutId);
    }

    @Override
    public void convert(BaseViewHolder holder, final PalBillItemDto bean, final int position) {
        holder.setText(R.id.tv_title, bean.getItemName() == null ? "" : bean.getItemName());
        // 二選一
        if (bean.getEnterType() == 1) {
            RadioGroup rg = holder.fd(R.id.rg_choice);
            rg.setVisibility(View.VISIBLE);
            RadioButton rbFirst = holder.fd(R.id.rb_first);
            RadioButton rbSecond = holder.fd(R.id.rb_second);
            String firstName = "";
            if (bean.getOpts() != null && bean.getOpts().size() > 1) {
                firstName = bean.getOpts().get(0).getOptName();
                rbFirst.setText(firstName);
                rbSecond.setText(bean.getOpts().get(1).getOptName());
            }
            if (bean.getDefVal().equals(firstName)) {
                rbFirst.setChecked(true);
            } else {
                rbSecond.setChecked(true);
            }
            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if (checkedId == R.id.rb_first) {
                        setRadioText(position, bean.getOpts().get(0).getItemId(), bean.getOpts().get(0).getOptName());
                    } else {
                        setRadioText(position, bean.getOpts().get(1).getItemId(), bean.getOpts().get(1).getOptName());
                    }
                }
            });
        } else if (bean.getEnterType() == 2) { // 多選一
            TextView tvRight = holder.fd(R.id.tv_right);
            tvRight.setVisibility(View.VISIBLE);
            tvRight.setText(bean.getDefVal());
        } else if (bean.getEnterType() == 3) { // 輸入值
            // 輸入時間
            if (bean.getEnterLimit() == 3) {
                TextView tvRight = holder.fd(R.id.tv_right);
                tvRight.setVisibility(View.VISIBLE);
                tvRight.setText(bean.getDefVal());
            } else {
                EditText et = holder.fd(R.id.et_input);
                et.setVisibility(View.VISIBLE);
                et.setText(bean.getDefVal());
                et.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        setInput(position, s.toString());
                    }
                });
                if (bean.getEnterLimit() == 2) {
                    et.setInputType(InputType.TYPE_CLASS_NUMBER);
                }
            }

        }
    }

    public void setChoiceText(int position, String id, String text) {
        setRadioText(position, id, text);
        notifyItemChanged(position);
    }

    public void setRadioText(int position, String id, String text) {
        list.get(position).setDefVal(text);
        list.get(position).setItemId(id);
    }

    public void setInput(int position, String time) {
        list.get(position).setDefVal(time);
    }
}
